package name.matthewgreet.example10.form; import java.util.Date; import java.util.List; import org.apache.commons.beanutils.PropertyUtils; import com.opensymphony.xwork2.TextProvider; import com.opensymphony.xwork2.interceptor.ValidationAware; import name.matthewgreet.example10.dto.DateRange; import name.matthewgreet.example10.dto.PrimeMinisterDTO; import name.matthewgreet.example10.dto.PrimeMinisterDTO.Party; import name.matthewgreet.example10.util.DateRangeListConverter; import name.matthewgreet.strutscommons.annotation.CustomCollectionConversion; import name.matthewgreet.strutscommons.annotation.DateConversion; import name.matthewgreet.strutscommons.annotation.IntegerConversion; import name.matthewgreet.strutscommons.annotation.Required; import name.matthewgreet.strutscommons.annotation.Trim; import name.matthewgreet.strutscommons.form.AbstractFormattableForm; @SuppressWarnings("deprecation") public class PrimeMinisterForm extends AbstractFormattableForm { private static final long serialVersionUID = -6937072630784482008L; @Trim @Required(message = "A name is required") private String name; @DateConversion(message = "Date of Birth, if set, must be in dd/mm/yyyy format") private Date dateOfBirth; @CustomCollectionConversion(message = "Terms of Office format is comma separated date ranges (dd/mm/yyyy-dd/mm/yyyy) though last date may be omitted", validatorClass = DateRangeListConverter.class) private List<DateRange> termsOfOffice; private Party party; @IntegerConversion(message = "Max Seats Won must be blank or a number") private Integer maxSeatsWon; public PrimeMinisterForm() { resetFields(); } public PrimeMinisterForm(PrimeMinisterDTO primeMinister) throws Exception { PropertyUtils.copyProperties(this, primeMinister); } @Override public void resetFields() { name = ""; dateOfBirth = null; termsOfOffice = null; party = null; maxSeatsWon = null; } @Override public void doValidate(ValidationAware validationAware, TextProvider textProvider) { // Empty } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(Date dateOfBirth) { this.dateOfBirth = dateOfBirth; } public List<DateRange> getTermsOfOffice() { return termsOfOffice; } public void setTermsOfOffice(List<DateRange> termsOfOffice) { this.termsOfOffice = termsOfOffice; } public Party getParty() { return party; } public void setParty(Party party) { this.party = party; } public Integer getMaxSeatsWon() { return maxSeatsWon; } public void setMaxSeatsWon(Integer maxSeatsWon) { this.maxSeatsWon = maxSeatsWon; } @Override public String toString() { final int maxLen = 30; return "PrimeMinisterForm [name=" + name + ", dateOfBirth=" + dateOfBirth + ", termsOfOffice=" + (termsOfOffice != null ? termsOfOffice.subList(0, Math.min(termsOfOffice.size(), maxLen)) : null) + ", party=" + party + ", maxSeatsWon=" + maxSeatsWon + "]"; } }